﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using FscNET.Client.Properties;
using Microsoft.Win32;
using CooLib;

namespace FscNET.Client
{
    
    static class Program
    {
        private static readonly string _assemblyVersion;
        private static readonly string _assemblyVersion2;
        private static readonly string _productTitle;
        private static CooRuntime _rt;

        public static string AssemblyVersion
        {
            get { return _assemblyVersion; }
        }
        public static string AssemblyVersion2
        {
            get { return _assemblyVersion2; }
        }
        public static string ProductTitle
        {
            get { return _productTitle; }
        }

        public static CooRuntime RT
        {
            get
            {
                if (_rt == null)
                    _rt = new CooRuntime();
                return _rt;
            }
        }

        public static CooTransaction TX
        {
            get
            {
                return RT.GetThreadTransaction();
            }
            set
            {
                if (value == null) throw new NullReferenceException();
                RT.SetThreadTransaction(value);
            }
        }

        static Program()
        {
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
            _assemblyVersion = AssemblyInfo.Version;
            _assemblyVersion2 = string.Join(".", AssemblyInfo.Version.Split(new char[] {'.'}), 0, 2);
            _productTitle = AssemblyInfo.Title + " v" + _assemblyVersion2;
        }

        

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Main());
        }

        static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            if (args.Name.StartsWith("coolibcs"))
            {
                try
                {
                    //find location of coolibcs from registry
                    RegistryKey reg_key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Fabasoft\\InstalledDLLs");
                    if (reg_key != null)
                    {
                        string[] dlls = reg_key.GetValueNames();

                        string path = Array.Find(dlls, delegate(string s) { return s.EndsWith("coolibcs.dll"); });
                        if (path != null)
                            return Assembly.LoadFrom(path);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(Resources.errUnableToLoadCoolibcs+ Environment.NewLine + e.Message);
                    return null;
                }
            }

            MessageBox.Show(string.Format(Resources.errUnableToLoadAssembly, args.Name), Resources.errError, 
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
            Application.Exit();
            return null;
        }
    }
}
